/*
  Copyright (c) 2014 Dimitrij Gester

  "Smart RC TX" is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  "Smart RC TX" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with "Smart RC TX"; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package com.codeplex.smartrctx.configurator.main;

import com.codeplex.smartrctx.configurator.main.controller.BaseSetter;
import com.codeplex.smartrctx.configurator.main.controller.ExpoSetter;
import com.codeplex.smartrctx.configurator.main.controller.LimiterSetter;
import com.codeplex.smartrctx.configurator.main.controller.PreScaleSetter;
import com.codeplex.smartrctx.configurator.main.controller.Setter;
import com.codeplex.smartrctx.configurator.main.controller.SimpleMixerSetter;
import com.codeplex.smartrctx.configurator.main.controller.TrimSetter;
import com.codeplex.smartrctx.configurator.main.fragments.EditorActionListener;
import com.codeplex.smartrctx.configurator.main.model.RCSettings;

import rcremote.main.R;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class OneChannelEditor extends Fragment implements FragmentConnection {
	View containerView;
	PreScaleSetter preScaleSetter;
	TrimSetter trimSetter;
	ExpoSetter expoSetter;
	SimpleMixerSetter mixerSetters[][];
	LimiterSetter limiterSetterPos, limiterSetterNeg;
	int currentChannel;

	public OneChannelEditor() {
		super();
	}

	public OneChannelEditor(int i) {
		super();
		setCurrentChannel(i);
	}

	public void setCurrentChannel(int channel) {
		if (channel < 0 || channel >= RCSettings.CHANNEL_COUNT)
			throw new IllegalArgumentException();

		currentChannel = channel;
		preScaleSetter.setChannel(channel);
		trimSetter.setChannel(channel);
		expoSetter.setChannel(channel);
		for (int i = 0; i < RCSettings.CHANNEL_COUNT; ++i) {
			mixerSetters[i][0].setChannel(channel);
			mixerSetters[i][1].setChannel(channel);
		}
		limiterSetterPos.setChannel(channel);
		limiterSetterNeg.setChannel(channel);
	}

	private void initializeView() {
		EditText e;

		// ----- pre-scale setter
		e = (EditText) containerView.findViewById(R.id.PreScalerEdit);
		preScaleSetter = new PreScaleSetter(e, currentChannel);
		attachModelSetter(e, preScaleSetter);

		// ----- trim setter
		e = (EditText) containerView.findViewById(R.id.TrimEdit);
		trimSetter = new TrimSetter(e, currentChannel);
		attachModelSetter(e, trimSetter);

		// ----- expo setter
		e = (EditText) containerView.findViewById(R.id.ExpoEdit);
		expoSetter = new ExpoSetter(e, currentChannel);
		attachModelSetter(e, expoSetter);

		// ----- mixer setter
		EditText mixerEdit[][] = new EditText[RCSettings.CHANNEL_COUNT][2];
		mixerSetters = new SimpleMixerSetter[RCSettings.CHANNEL_COUNT][2];

		mixerEdit[0][0] = (EditText) containerView
				.findViewById(R.id.MixerEdit1Pos);
		mixerEdit[1][0] = (EditText) containerView
				.findViewById(R.id.MixerEdit2Pos);
		mixerEdit[2][0] = (EditText) containerView
				.findViewById(R.id.MixerEdit3Pos);
		mixerEdit[3][0] = (EditText) containerView
				.findViewById(R.id.MixerEdit4Pos);
		mixerEdit[4][0] = (EditText) containerView
				.findViewById(R.id.MixerEdit5Pos);
		mixerEdit[5][0] = (EditText) containerView
				.findViewById(R.id.MixerEdit6Pos);
		mixerEdit[0][1] = (EditText) containerView
				.findViewById(R.id.MixerEdit1Neg);
		mixerEdit[1][1] = (EditText) containerView
				.findViewById(R.id.MixerEdit2Neg);
		mixerEdit[2][1] = (EditText) containerView
				.findViewById(R.id.MixerEdit3Neg);
		mixerEdit[3][1] = (EditText) containerView
				.findViewById(R.id.MixerEdit4Neg);
		mixerEdit[4][1] = (EditText) containerView
				.findViewById(R.id.MixerEdit5Neg);
		mixerEdit[5][1] = (EditText) containerView
				.findViewById(R.id.MixerEdit6Neg);

		for (int i = 0; i < RCSettings.CHANNEL_COUNT; ++i) {
			mixerSetters[i][0] = new SimpleMixerSetter(mixerEdit[i][0],
					currentChannel, i, 0);
			attachModelSetter(mixerEdit[i][0], mixerSetters[i][0]);

			mixerSetters[i][1] = new SimpleMixerSetter(mixerEdit[i][1],
					currentChannel, i, 1);
			attachModelSetter(mixerEdit[i][1], mixerSetters[i][1]);
		}

		// ----- limiter setter
		e = (EditText) containerView.findViewById(R.id.LimiterEditPos);
		limiterSetterPos = new LimiterSetter(e, currentChannel, 0);
		attachModelSetter(e, limiterSetterPos);

		e = (EditText) containerView.findViewById(R.id.LimiterEditNeg);
		limiterSetterNeg = new LimiterSetter(e, currentChannel, 1);
		attachModelSetter(e, limiterSetterNeg);
	}

	private void attachModelSetter(EditText e, BaseSetter s) {
		RCSettings.getSettings().addObserver(s);

		addActionListener(e, s);
	}

	private void addActionListener(EditText e, Setter s) {
		EditorActionListener l = new EditorActionListener(this, s);
		e.setOnEditorActionListener(l);
		e.setOnFocusChangeListener(l);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		containerView = inflater.inflate(R.layout.one_channel_editor_fragment,
				container);

		initializeView();

		return containerView;
	}

	@Override
	public Activity getContainingActivity() {
		return getActivity();
	}

	@Override
	public View getContainerView() {
		return containerView;
	}

	@Override
	public void onValueChanged(View originator, String val) {
	}
}
